//
//  UIView+Constraint.m
//  YunDongGym
//
//  Created by lqq on 2018/5/24.
//  Copyright © 2018年 lqq. All rights reserved.
//

#import "UIView+Constraint.h"

@implementation UIView (Constraint)
#pragma mark 添加UIView
/**
 添加UIView
 */
- (UIView *)addViewWithEdgeInsets:(UIEdgeInsets)edgeInsets backColor:(UIColor *)backColor
{
    UIView *subView = [[UIView alloc] init];
    subView.backgroundColor = backColor;
    [self addSubview:subView];
    [subView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(edgeInsets.top);
        make.left.equalTo(self).offset(edgeInsets.left);
        make.right.equalTo(self).offset(edgeInsets.right);
        make.bottom.equalTo(self).offset(edgeInsets.bottom);
    }];
    return subView;
}

/**
 添加View，固定高度在顶部
 */
- (UIView *)addViewKeepTopWithHeight:(CGFloat)height topSpace:(CGFloat)topSpace leftSpace:(CGFloat)leftSpace rightSpace:(CGFloat)rightSpace backColor:(UIColor *)backColor
{
    UIView *subView = [[UIView alloc] init];
    subView.backgroundColor = backColor;
    [self addSubview:subView];
    [subView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(topSpace);
        make.left.equalTo(self).offset(leftSpace);
        make.right.equalTo(self).offset(-rightSpace);
        make.height.equalTo(@(height));
    }];
    return subView;
}

/**
 添加View，固定高度在底部
 */
- (UIView *)addViewKeepBottomWithHeight:(CGFloat)height bottomSpace:(CGFloat)bottomSpace leftSpace:(CGFloat)leftSpace rightSpace:(CGFloat)rightSpace backColor:(UIColor *)backColor
{
    UIView *subView = [[UIView alloc] init];
    subView.backgroundColor = backColor;
    [self addSubview:subView];
    [subView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(-bottomSpace);
        make.left.equalTo(self).offset(leftSpace);
        make.right.equalTo(self).offset(-rightSpace);
        make.height.equalTo(@(height));
    }];
    return subView;
}

/**
 添加View，固定高度在左边
 */
- (UIView *)addViewKeppLeftWithWidth:(CGFloat)width leftSpace:(CGFloat)leftSpace topSpace:(CGFloat)topSpace bottomSpace:(CGFloat)bottomSpace backColor:(UIColor *)backColor
{
    UIView *subView = [[UIView alloc] init];
    subView.backgroundColor = backColor;
    [self addSubview:subView];
    [subView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(topSpace);
        make.left.equalTo(self).offset(leftSpace);
        make.bottom.equalTo(self).offset(-bottomSpace);
        make.width.equalTo(@(width));
    }];
    return subView;
}

/**
 添加View，固定高度在右边
 */
- (UIView *)addViewKeepRightWithWidth:(CGFloat)width rightSpace:(CGFloat)rightSpace topSpace:(CGFloat)topSpace bottomSpace:(CGFloat)bottomSpace backColor:(UIColor *)backColor
{
    UIView *subView = [[UIView alloc] init];
    subView.backgroundColor = backColor;
    [self addSubview:subView];
    [subView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(topSpace);
        make.right.equalTo(self).offset(-rightSpace);
        make.bottom.equalTo(self).offset(-bottomSpace);
        make.width.equalTo(@(width));
    }];
    return subView;
}

/**
 添加底部横线
 */
- (UIView *)addBottomLineViewWithLeftSpace:(CGFloat)leftSpace rightSpace:(CGFloat)rightSpace
{
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor =[UIColor colorWithRed:229.0 / 255 green:229.0 / 255 blue:229.0 / 255 alpha:1];
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
        make.left.equalTo(self).offset(leftSpace);
        make.right.equalTo(self).offset(-rightSpace);
        make.height.equalTo(@(kOnePX));
    }];
    return lineView;
}


#pragma mark 添加UILabel
/**
 添加UILabel居中
 */
- (UILabel *)addCenterLabelWithText:(NSString *)text
                            textColor:(UIColor *)textColor
                                 font:(UIFont *)font
{
    UILabel *subLabel = [[UILabel alloc] init];
    subLabel.text = text;
    subLabel.textColor = textColor;
    subLabel.font = font;
    [self addSubview:subLabel];
    [subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.centerX.equalTo(self);
    }];
    return subLabel;
}


/**
 添加UILabel垂直居中靠左
 */
- (UILabel *)addVCenterLabelKeepLeft:(CGFloat)leftSpace
                                            text:(NSString *)text
                                       textColor:(UIColor *)textColor
                                            font:(UIFont *)font
{
    UILabel *subLabel = [[UILabel alloc] init];
    subLabel.text = text;
    subLabel.textColor = textColor;
    subLabel.font = font;
    [self addSubview:subLabel];
    [subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self).offset(leftSpace);
    }];
    return subLabel;
}

/**
 添加UILabel垂直居中靠右
 */
- (UILabel *)addVCenterLabelKeepRight:(CGFloat)rightSpace
                                             text:(NSString *)text
                                        textColor:(UIColor *)textColor
                                             font:(UIFont *)font
{
    UILabel *subLabel = [[UILabel alloc] init];
    subLabel.text = text;
    subLabel.textColor = textColor;
    subLabel.font = font;
    [self addSubview:subLabel];
    [subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self).offset(-rightSpace);
    }];
    return subLabel;
}

/**
 添加label靠上和左
 */
- (UILabel *)addLabelKeepTop:(CGFloat)topSpace
                        left:(CGFloat)leftSpace
                        text:(NSString *)text
                   textColor:(UIColor *)textColor
                        font:(UIFont *)font
{
    UILabel *subLabel = [[UILabel alloc] init];
    subLabel.text = text;
    subLabel.textColor = textColor;
    subLabel.font = font;
    [self addSubview:subLabel];
    [subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(topSpace);
        make.left.equalTo(self).offset(leftSpace);
    }];
    return subLabel;
}

#pragma mark 添加UIImageView
/**
 添加UIImageView垂直居中靠左
 */
- (UIImageView *)addVCenterImageViewKeepLeft:(CGFloat)leftSpace
                                       width:(CGFloat)width
                                      height:(CGFloat)height
                                   imageName:(NSString *)imageName
{
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = IMG_NAME(imageName);
    [self addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self).offset(leftSpace);
        make.width.equalTo(@(width));
        make.height.equalTo(@(height));
    }];
    return imageView;
}
/**
 添加UIImageView垂直居中靠右
 */
- (UIImageView *)addVCenterImageViewKeepRight:(CGFloat)rightSpace
                                        width:(CGFloat)width
                                       height:(CGFloat)height
                                    imageName:(NSString *)imageName
{
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = IMG_NAME(imageName);
    [self addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self).offset(-rightSpace);
        make.width.equalTo(@(width));
        make.height.equalTo(@(height));
    }];
    return imageView;
}







@end
